const ModalTypes = {
  SUCCESS: "success",
  WARNING: "warning",
  DANGER: "danger",
};

class Modal {
  constructor(status) {
    this.status = status;
  }
  get className() {
    let classStr = "modal ";
    switch (this.status) {
      case ModalTypes.SUCCESS:
        classStr += "success";
        break;
      case ModalTypes.WARNING:
        classStr += "warning";
        break;
      case ModalTypes.DANGER:
        classStr += "danger";
        break;
      default:
        break;
    }
    return classStr;
  }
  static checkStatusIsEist(types, status) {
    for (let t in types) {
      if (types[t] === status) {
        return true;
      }
    }
    return false;
  }
}

class SuccessModal extends Modal {
  constructor(title) {
    super(ModalTypes.SUCCESS);
    this.title = "成功:" + title;
  }
}
class WarningModal extends Modal {
  constructor(title) {
    super(ModalTypes.WARNING);
    this.title = "警告:" + title;
  }
}
class DangerModal extends Modal {
  constructor(title) {
    super(ModalTypes.DANGER);
    this.title = "失败:" + title;
  }
}

class ModalFactory {
  constructor(dom) {
    this.dom = dom;
  }
  create(title, status) {
    const statusIsEist = Modal.checkStatusIsEist(ModalTypes, status);
    if (!statusIsEist) {
      throw new Error("Modal type is incorrect");
    }
    const dom = this.dom;
    let modal = null;
    switch (status) {
      case ModalTypes.SUCCESS:
        modal = new SuccessModal(title);
        break;
      case ModalTypes.WARNING:
        modal = new WarningModal(title);
        break;
      case ModalTypes.DANGER:
        modal = new DangerModal(title);
        break;
      default:
        break;
    }

    dom.getElementsByTagName("header")[0].innerText = modal.title;
    dom.className = modal.className;
  }
}

export default ModalFactory;
